package com.hxkj.service.impl;

import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.MessageServiceClient;
import com.hxkj.client.service.UserInfoServiceClient;
import com.hxkj.domain.po.LastestMessage;
import com.hxkj.domain.po.Message;
import com.hxkj.domain.po.SysUser;
import com.hxkj.domain.vo.MessageVO;
import com.hxkj.service.MessageService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class MessageServiceImpl implements MessageService{

    @Autowired
    private MessageServiceClient msClient;
    @Autowired
    private UserInfoServiceClient userInfoServiceClient;

    @Override
    public PageInfo<LastestMessage> selectMessageList(LastestMessage lastestMessage, PageTo pageParam, HttpServletRequest request) throws Exception {
        SysUser user = getUserByToken(request);

        lastestMessage.setSenderId(user.getUserId());
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ParameterDataStruct<LastestMessage> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(lastestMessage);
        parameterDataStruct.setPageTo(pageParam);
        return msClient.getList(parameterDataStruct);
    }

    @Override
    public PageInfo<MessageVO> getMessageChat(Message message, PageTo pageParam, HttpServletRequest request) throws Exception {
        SysUser user = getUserByToken(request);

        if(message.getMessageId() == null)
            throw new CustomMessageException("站内信编号不能为空");

        ResultDataStruct<Message> res = msClient.getMessageById(message.getMessageId());
        if(res.getCode() != 200){
            throw new CustomMessageException(res.getMessage());
        }
        message = res.getData();
        if(message == null){
            throw new CustomMessageException("站内信记录不存在");
        }
        /**
         * 把发送人调整成当前登录用户
         * 主要是为了后面业务处理方便
         * 具体效果查看SQL语句
         */
        //message.setSenderId(user.getUserId());
        if(!user.getUserId().equals(message.getSenderId())){
            String receiver = message.getSenderId();
            message.setSenderId(user.getUserId());
            message.setReceiverId(receiver);
        }
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ParameterDataStruct<Message> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(message);
        parameterDataStruct.setPageTo(pageParam);
        ResultDataStruct<PageInfo<MessageVO>> result = msClient.getMessageChat(parameterDataStruct);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
        return result.getData();
    }

    @Override
    public void sendMessage(Message message, HttpServletRequest request) throws Exception {
        SysUser user = getUserByToken(request);

        if(StringUtils.isBlank(message.getReceiverAccount())){
            throw new CustomMessageException("收信人不能为空");
        }
        if(StringUtils.isBlank(message.getMessageContent())){
            throw new CustomMessageException("站内信内容不能为空");
        }
        if(message.getReceiverAccount().equals(user.getUserAccount())){
            throw new CustomMessageException("不能给自己发消息");
        }
        
        message.setSenderId(user.getUserId());
        message.setSenderAccount(user.getUserAccount());
        ResultMessageStruct result = msClient.sendMessage(message);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
    }

    @Override
    public void batchDeleteMessage(String ids, HttpServletRequest request) throws Exception {
        SysUser user = getUserByToken(request);

        String userId = user.getUserId();//request.getHeader("userId");

        Map<String, Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("ids",ids);

        ResultMessageStruct result = msClient.batchDeleteMessage(map);
        if(result.getCode() != 200)
            throw new CustomMessageException(result.getMessage());

    }

    @Override
    public void batchModifyMessageStatus(String ids, HttpServletRequest request) throws Exception {
        SysUser user = getUserByToken(request);

        String userId = user.getUserId();//request.getHeader("userId");

        Map<String, Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("ids",ids);

        ResultMessageStruct result = msClient.batchModifyMessageStatus(map);
        if(result.getCode() != 200)
            throw new CustomMessageException(result.getMessage());
    }

    @Override
    public void deleteMessage(String messageId, HttpServletRequest request) throws Exception{
        SysUser user = getUserByToken(request);
        if(StringUtils.isBlank(messageId)){
            throw new CustomMessageException("消息编号不能为空");
        }
        ResultDataStruct<Message> result = msClient.getMessageById(Long.valueOf(messageId));
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
        Message message = result.getData();
        if(message.getSenderId().equals(user.getUserId())){
            message.setSendDelFlag(1);
        }else if(message.getReceiverId().equals(user.getUserId())){
            message.setReceiveDelFlag(1);
        }
        ResultMessageStruct re = msClient.deleteMessage(message);
        if(re.getCode() != 200){
            throw new CustomMessageException(re.getMessage());
        }
    }

    public SysUser getUserByToken(HttpServletRequest request) throws Exception{
        String accessToken = request.getHeader("accessToken");

        if (StringUtils.isEmpty(accessToken))
            throw new CustomException("token为空");

        ResultDataStruct<SysUser> resultDataStruct = userInfoServiceClient.userinfoGet(accessToken);
        if (resultDataStruct.getCode() != 200)
            throw new CustomException(resultDataStruct.getMessage());

        SysUser user = resultDataStruct.getData();
        return user;
    }
}
